<template>
	<section style="height:500px; overflow: auto;" v-loading="commonLoading">
		<el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
			<el-form-item label="配置名称" prop="title">
				<el-input v-model="ruleForm.title"></el-input>
			</el-form-item>
			<el-form-item label="配置标签" prop="key">
				<el-input v-model="ruleForm.key"></el-input>
			</el-form-item>
			<el-form-item label="配置值" prop="value">
				<el-input v-model="ruleForm.value"></el-input>
			</el-form-item>
			<el-form-item label="状态" prop="status">
				<el-switch 
				    v-model="ruleForm.status" 
					active-value="1"
                    inactive-value="2"></el-switch>
			</el-form-item>
			<el-form-item label="配置说明" prop="remark">
				<el-input v-model="ruleForm.remark"></el-input>
			</el-form-item>
			<el-form-item style="position: absolute;bottom: 0;">
				<el-button type="primary" @click="toSave('ruleForm')">提交保存</el-button>
				<!-- <el-button @click="resetForm('ruleForm')">重置</el-button> -->
			</el-form-item>
		</el-form>
	</section>
</template>
<script>
export default {
	data() {
	    return {
	    	//此处不能再次定义props的属性了
	    	ruleForm:{
	    		id:0,
	    		title:'',
	    		key:'',
	    		value:'',
	    		status:"1",
	    		remark:''
	    	},
	    	commonLoading:false,
	    	buttonLoading:false,
	    	rules:{
	    		title: [ { required: true, message: '请输入配置名称', trigger: 'blur' },],
	    		key: [ { required: true, message: '请输入配置标签', trigger: 'blur' },],
	    		value: [ { required: true, message: '请输入配置值', trigger: 'blur' },],
	    	}
	    }
	},props:{
		//接受父级传的值属性
        conf_id:0
    },methods:{
    	toSave(formName){
    		this.$refs[formName].validate((valid) => {
				if (valid) {

		    		if(this.conf_id>0){
		    			var url = '/admin/conf/edit';
		    		}else{
		    			var url = '/admin/conf/add';
		    		}
		    		
		            var token = this.Common.cookie('token');
		            var data = this.ruleForm;
		            data.token = token;
		            var tar = this;
		            tar.commonLoading = true;
					tar.Common.request(url, data, function(data){
			        	tar.commonLoading = false;
			        	if(data.success_code==0){
			        		tar.$message({
								title: '警告',
								message: data.message,
								type: 'error'
							});
			        	}else{
			        		tar.$message({
								title: '成功',
								message: data.message,
								type: 'success'
							});
							//调用父类关闭当前窗口
							tar.$emit('closeDialog')
			        	}
			        },function(data){
			        	var message = '接口请求失败';
			        	if(data.message){
			        		message = data.message;
			        	}
			        	tar.commonLoading = false;
						tar.$message({
							title: '警告',
							message: message,
							type: 'error'
						});
			        },this);
				}else{
					console.log('error submit!!');
					return false;
				}
			})
    	},getInfo(){
			var url = '/admin/conf/getInfo';
            var token = this.Common.cookie('token');
            var data = {'token':token,'id':this.conf_id}
            var tar = this;
            tar.commonLoading = true;
			tar.Common.request(url, data, function(data){
	        	tar.commonLoading = false;
	        	if(data.success_code==0){
	        		tar.$message({
						title: '警告',
						message: data.message,
						type: 'error'
					});
	        	}else{
	        		data.result.status += "";
	        		tar.ruleForm = data.result;
	        	}
	        },function(data){
	        	var message = '接口请求失败';
	        	if(data.message){
	        		message = data.message;
	        	}
	        	tar.commonLoading = false;
				tar.$message({
					title: '警告',
					message: message,
					type: 'error'
				});
	        },this);
		},resetForm(formName) {
			//重置
        	this.$refs[formName].resetFields();
      	}
    },mounted(){
    	if(this.conf_id>0){
    		this.getInfo();
    	}
	}
}
</script>